/*
 * Copyright (c) 2013 Hai Bison
 *
 * See the file LICENSE at the root directory of this project for copying
 * permission.
 */

package com.haibison.android.logs.providers.log;

import static android.provider.BaseColumns._ID;
import static com.haibison.android.logs.providers.BaseCols._DATE_CREATED;
import static com.haibison.android.logs.providers.BaseCols._DATE_MODIFIED;
import static com.haibison.android.logs.providers.log.LogContract.COLUMN_LEVEL;
import static com.haibison.android.logs.providers.log.LogContract.COLUMN_MESSAGE;
import static com.haibison.android.logs.providers.log.LogContract.COLUMN_TAG;
import static com.haibison.android.logs.providers.log.LogContract.COLUMN_THROWABLE;
import static com.haibison.android.logs.providers.log.LogContract.LEVEL_DEBUG;
import static com.haibison.android.logs.providers.log.LogContract.TABLE_NAME;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.haibison.android.logs.Env;
import com.haibison.android.logs.providers.DbUtils;

/**
 * SQLite helper for {@link LogProvider}.
 * 
 * @author Hai Bison
 * @since v1.0 beta
 */
public class LogHelper extends SQLiteOpenHelper {

    /**
     * Database filename.
     */
    public static final String DB_FILENAME = Env.getEnvName("logs.db");

    /**
     * Database version.
     */
    public static final int DB_VERSION = 1;

    /**
     * SQL creator.
     */
    private static final String DB_CREATOR = "CREATE TABLE " + TABLE_NAME
            + " ( " + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + _DATE_CREATED + " TEXT NOT NULL DEFAULT "
            + DbUtils.formatNumber(0l, true) + ", " + _DATE_MODIFIED
            + " TEXT NOT NULL DEFAULT " + DbUtils.formatNumber(0l, true) + ", "
            + COLUMN_LEVEL + " INTEGER NOT NULL DEFAULT " + LEVEL_DEBUG + ", "
            + COLUMN_TAG + " TEXT, " + COLUMN_MESSAGE + " TEXT, "
            + COLUMN_THROWABLE + " TEXT)";

    /**
     * Creates new instance.
     * 
     * @param context
     *            the context.
     */
    public LogHelper(Context context) {
        super(context, DB_FILENAME, null, DB_VERSION);
    }// MessageHelper()

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DB_CREATOR);
    }// onCreate()

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        /*
         * Do nothing.
         */
    }// onUpgrade()

}
